﻿using System;
using System.Web.Mvc;
using QDT.Core.Domain;
using QDT.MVC.Security;
using QDT.Web.Admin.Helper;
using QDT.Web.Admin.Models;

namespace QDT.Web.Admin.Controllers {
    //[SecurityModule(Name = "导航管理")]
    public class MenuNavController : BaseController {
        //[SecurityNode(Name = "导航列表")]
        public ActionResult Index() {
            return View();
        }
        //[SecurityNode(Name = "新增页面")]
        public ActionResult Add() {
            return View();
        }
        [HttpPost]
        //[SecurityNode(Name = "新增")]
        public ActionResult PostAdd(NavModel model) {
            var entity = model.ToEntity();
            DbProvider.Add(entity);
            DbProvider.SaveChanges();
            return Success("操作成功");
        }
        //[SecurityNode(Name = "编辑页面")]
        public ActionResult Edit(int id) {
            var data = DbProvider.GetByID<MenuNav>(id);
            var model = data.ToModel();
            return View(model);
        }
        //[SecurityNode(Name = "编辑")]
        public ActionResult PostEdit(NavModel model) {
            var data = DbProvider.GetByID<MenuNav>(model.Id);
            data.Name = model.Name;
            data.ParentId = model.ParentId != null && model.ParentId.Value == 0 ? null : model.ParentId;
            data.Url = model.Url;
            data.AppIndex = model.AppIndex;
            data.IsEffect = model.IsEffect;
            data.UParam = model.UParam;
            data.IsBlank = model.IsBlank;
            data.IsShop = model.IsShop;
            data.UModule = model.UModule;
            data.Sort = model.Sort;
            data.UAction = model.UAction;
            data.UId = model.UId;
            DbProvider.Update(data);
            DbProvider.SaveChanges();
            return Success("操作成功");
        }
        //[SecurityNode(Name = "彻底删除")]
        public JsonResult ForeverDel(int[] id)
        {
            try {
                foreach(var i in id) {
                    int tmpId = i;
                    DbProvider.Delete<MenuNav>(o=>o.ID == tmpId);
                }
                DbProvider.SaveChanges();
                return Json(new {
                    Status = true, Info = "操作成功"
                });
            }catch(Exception ex) {
                return Json(new {
                    Status = false, Info = ex.Message
                });
            }
        }
    }
}